package rx.genarator.comprehension;

import java.util.HashSet;
import java.util.Set;

public class DataBase {
	
	private String name;
	private String code;
	private String comment;
	private String id;
	
	private Set<Table> tables = new HashSet<Table>();

	private Set<View> views = new HashSet<View>();
	
	public String getCode() {
		return code;
	}

	public String getComment() {
		return comment;
	}

	public String getName() {
		return name;
	}

	public DataBase(String id,String name, String code, String comment) {
		super();
		if(id == null || name == null || code == null)
			throw new IllegalArgumentException("id:" + id + ",name:" + name + ",code:" + code + ",comment:" + comment);
		this.id = id;
		this.name = name;
		this.code = code;
		this.comment = comment;
		ObjectsMap.put(id, this);
	}
	
	public synchronized void addTable(Table table){
		this.tables.add(table);
	}
	public synchronized void addTables(Set<Table> tables){
		this.tables.addAll(tables);
	}
	public synchronized boolean removeTable(Table table){
		return this.tables.remove(table);
	}
	
	public synchronized boolean removeTables(Set<Table> tables){
		return this.tables.removeAll(tables);
	}
	
	public synchronized void addView(View view){
		this.views.add(view);
	}
	public synchronized void addViews(Set<View> views){
		this.views.addAll(views);
	}
	public synchronized boolean removeView(View view){
		return this.views.remove(view);
	}
	
	public synchronized boolean removeViews(Set<View> views){
		return this.views.removeAll(views);
	}

	public String getId() {
		return id;
	}
	
	public  Set<Table> getTables(){
		return new HashSet<Table>(this.tables);
	}
	
	public  Set<View> getView(){
		return new HashSet<View>(this.views);
	}

	public Table createTable(String id,String name,String code,String comment){
		Table table = new Table(this,id,name,code,comment);
		this.addTable(table);
		return table;
	}
	
	public View createView(String id,String name,String code,String comment){
		View view = new View(this,id,name,code,comment);
		this.addView(view);
		return view;
	}
}
